/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* reverseKGroup(ListNode* head, int k) {
        int n=0;
        ListNode* a=head;
        while(a)
        {
            a=a->next;
            n++;
        }
        n/=k;
        ListNode* newhead=new ListNode(0);
        ListNode* cur=newhead;
         ListNode* last=head;
        for(int i=0;i<n;++i)
        {
            ListNode* tmp=last;
            for(int j=0;j<k;++j)
            {
                ListNode* _next=last->next;
                last->next=cur->next;
                cur->next=last;
                last=_next;
            }
            cur=tmp;
        }
        cur->next=last;
        ListNode* b=newhead->next;
        delete newhead;
        return b;
    }
};
